﻿using System;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// 安全帮助类，提供密码加密功能
/// </summary>
public static class SecurityHelper
{
    /// <summary>
    /// 使用SHA256算法哈希密码
    /// </summary>
    public static string HashPassword(string password)
    {
        using (var sha256 = SHA256.Create())
        {
            byte[] hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));
            return BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
        }
    }

    /// <summary>
    /// 生成随机盐值
    /// </summary>
    public static string GenerateSalt()
    {
        byte[] saltBytes = new byte[16];
        using (var rng = RandomNumberGenerator.Create())
        {
            rng.GetBytes(saltBytes);
        }
        return Convert.ToBase64String(saltBytes);
    }

    /// <summary>
    /// 使用盐值哈希密码
    /// </summary>
    public static string HashPasswordWithSalt(string password, string salt)
    {
        string saltedPassword = password + salt;
        return HashPassword(saltedPassword);
    }
}